package net.towee.client.widgets;

import net.towee.client.resources.ClientResource;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class LeftTabsWidget extends UnorderedListWidget implements ClickHandler {
	protected final ClientResource resource;
	
	private FlowPanel arrow;
	private Widget selectedCat;
	
	public LeftTabsWidget(ClientResource resource) {
		this.arrow = new FlowPanel();
		this.selectedCat = new FlowPanel();
		this.resource = resource;
		
		// Set styles
		setStyleName(resource.css().lobbyProfileCategoriesContainer());
		arrow.setStyleName(resource.css().lobbyProfileRightArrow());
		arrow.setStyleName(resource.css().lobbyProfileRightArrow());
	}

	public Widget addTab(String name) {
		return addTab(name, false);
	}
	
	public Widget addTab(String name, boolean selected) {
		ListItemWidget item = new ListItemWidget( name );
		item.setStyleName(resource.css().lobbyProfileCategory());
		add(item);
		item.addClickHandler(this);
		
		if(selected) 
			selectTab(item);
		
		return item;
	}
	
	@Override
	public void onClick(ClickEvent event) {
		Object source = event.getSource();
		selectTab((Widget)source);
	}
	
	public void selectTab(Widget w) {
		if(selectedCat!=null && !selectedCat.equals(w)){
			selectedCat.removeStyleName( resource.css().lobbyProfileCategorySelected() );
			arrow.removeFromParent();
		}
			
		((Widget)w).addStyleName( resource.css().lobbyProfileCategorySelected() );
		((HasWidgets)w).add(arrow);
		selectedCat = ((Widget)w);		
	}
}
